package org.veridiandynamics.client.view;


import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.domain.Admin;
import org.veridiandynamics.server.domain.Doctor;
import org.veridiandynamics.server.domain.Nurse;
import org.veridiandynamics.server.domain.Patient;
import org.veridiandynamics.server.domain.RoleLookup;
import org.veridiandynamics.server.domain.Roles;
import org.veridiandynamics.server.domain.User;


/**
 * This class merely brings up the login screen and handles the login process.
 * 
 * @author Lee
 */
public class LoginScreen {

	private JFrame frame;
	private JTextField userText;
	private JPasswordField passText;
	public static String[] roles;
	private JComboBox roleSelector;
	public static final int XCENTER = Toolkit.getDefaultToolkit().getScreenSize().width/2;
	public static final int YCENTER = Toolkit.getDefaultToolkit().getScreenSize().height/2;


	private HashMap<String, Object> loginMap;

	/**
	 * Constructor for login screen panels - initializes the contents of the frame.
	 */
	public LoginScreen()
	{
		initialize();
	}

	/**
	 * Closes the loginscreen window
	 */
	public void dispose()
	{
		frame.dispose();
	}

	/**
	 * Initializes the contents of the frame.
	 */
	public void initialize() 
	{
		frame = new JFrame("Login");
		frame.setBounds(XCENTER - 150, YCENTER - 75, 300, 150);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setLayout(new GridLayout(4, 2));
		Color myColor = new Color(200, 200, 200);
		frame.setBackground(myColor);

		JLabel userID = new JLabel("  UserId:");
		JLabel passW = new JLabel("  Password:");
		JLabel drop = new JLabel("  Please select your role:");
		userText = new JTextField(15);
		passText = new JPasswordField(15);

		JButton loginButton = new JButton("Login");
		loginButton.addActionListener(new ActionListener() 
		{
			/**
			 * This method handles the loggin in process.
			 */
			public void actionPerformed(ActionEvent e) 
			{
				String role = roles[roleSelector.getSelectedIndex()];
				String username = userText.getText();
				String password = passText.getText();

				//ClientConnectionHandler handler = new ClientConnectionHandler();
				//				
				//User loginUser = handler.login(userText.getText(), passText.getText(), role);

				//if (loginUser != null) 

				HibernateDriver driver = new HibernateDriver();
				Object logdummy = new Object();
				if(role.equals("Patient")){
					logdummy=new Patient();
					logdummy=(Patient)driver.LoginRetrieve(username, password, new Roles(RoleLookup.Role.PATIENT));
				} else  if(role.equals("Nurse")){
					logdummy=new Nurse();
					logdummy=(Nurse)driver.LoginRetrieve(username, password, new Roles(RoleLookup.Role.NURSE));
				} else  if(role.equals("Doctor")){
					logdummy=new Doctor();
					logdummy=(Doctor)driver.LoginRetrieve(username, password, new Roles(RoleLookup.Role.DOCTOR));
				} else {
					logdummy=new Admin();
					logdummy=(Admin)driver.LoginRetrieve(username, password, new Roles(RoleLookup.Role.ADMIN)); 
				}
				try { 
					if (password.equals(((User) logdummy).getPassword()))
					{
						if(!((User) logdummy).isSuspended())
						{
							JOptionPane.showMessageDialog(null, "Login Success");
							frame.dispose();
							MainWindow window = new MainWindow(roleSelector.getSelectedIndex(), 
									userText.getText(), logdummy);
						}
					}
				} catch(NullPointerException e1) 
				{
					JOptionPane.showMessageDialog(null, "Login failed");
				}
			}
		});

		JButton forgotButton = new JButton("Forgot Password?");
		roles = new String[4];
		roles[0] = "Patient";
		roles[1] = "Nurse";
		roles[2] = "System Administrator";
		roles[3] = "Doctor";
		roleSelector = new JComboBox(roles);

		frame.add(userID);
		frame.add(userText);
		frame.add(passW);
		frame.add(passText);
		frame.add(drop);
		frame.add(roleSelector);	
		frame.add(loginButton);
		frame.add(forgotButton);  

		frame.setVisible(true);
	}	

}	
